/*
Copyright 2010  Murali Devi (profdevi@gmail.com)

This file is part of ComineGL Project.

ComineGL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ComineGL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ComineGL.  If not, see <http://www.gnu.org/licenses/>.

This code uses the irrlicht and irrklang libraries.  Please read
the libraries copyrights at 

	irrlicht library:  http://irrlicht.sourceforge.net
	irrklang library:  http://www.ambiera.com/irrklang

*/



//v0.14 copyright Comine.com 20101216R20
#include "MStdLib.h"
#include "Global.h"
#include "ComineGLTexture.h"



//*********************************************
//** Exported Functions
//*********************************************
MDLLFUN bool CGLTextureLoad(int textureid,const char *texturefile)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	// Remove Texture if it exists already in table
	if(GlobalTextureList.IsKey(textureid)==true)
		{
		GlobalTextureList.Remove(textureid);
		MStdPrintf("Removing existing texture %d\n",textureid);
		}

	// Load up the file from memory

	TRefCount<MCGLTextureInfo> *newtexture=new TRefCount<MCGLTextureInfo>;
	if(newtexture==NULL)
		{
		MStdPrintf("**Unable to alloc memory for Texture\n");
		return false;
		}

	if(newtexture->Create(texturefile,GlobalRefVideoDriver)==false)
		{
		newtexture->Drop();
		MStdPrintf("**Unable to load texture from file %s\n",texturefile);
		return false;
		}

	if(GlobalTextureList.Add(textureid,newtexture)==false)
		{
		newtexture->Drop();
		return false;
		}
	
	// Drop Reference to texture
	newtexture->Drop();
	return true;
	}


//////////////////////////////////////////////////
MDLLFUN bool CGLTextureExists(int textureid)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}
	
	return GlobalTextureList.IsKey(textureid);		
	}


//////////////////////////////////////////////////
MDLLFUN bool CGLTextureUnload(int textureid)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	if(GlobalTextureList.Remove(textureid)==false)
		{
		return false;
		}
	
	return true;
	}


/////////////////////////////////////////////////
MDLLFUN bool CGLTextureUnloadAll(void)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	if(GlobalTextureList.Clear()==false)
		{
		return false;
		}

	return true;	
	}


/////////////////////////////////////////////////
MDLLFUN int CGLTextureGetCount(void)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return 0;
		}

	return GlobalTextureList.GetCount();
	}


/////////////////////////////////////////////////
MDLLFUN bool CGLTextureSet(int texid,int nodeid,int layer)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	// Find Node
	MCGLNodeInfo *node=GlobalNodeList.Find(nodeid);
	if(node==NULL)
		{
		MStdPrintf("**Unable to find node with id=%d\n",nodeid);
		return false;
		}

	MCGLTextureInfo *texture=GlobalTextureList.Find(texid);
	if(texture==NULL)
		{
		return false;
		}

	ISceneNode *scenenode=node->GetNode();
	MStdAssert(scenenode!=NULL);

	ITexture *textureptr=texture->GetTexture();
	MStdAssert(textureptr!=NULL);

	scenenode->setMaterialTexture(layer,textureptr);
	
	return true;
	}
	

/////////////////////////////////////////////////
MDLLFUN bool CGLTextureColorKey(int textureid,int alpha,int red,int green,int blue)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	MCGLTextureInfo *texture=GlobalTextureList.Find(textureid);
	if(texture==NULL)
		{
		MStdPrintf("**Unable to find texture %d\n",textureid);
		return false;
		}

	ITexture *itexture=texture->GetTexture();
	GlobalRefVideoDriver->makeColorKeyTexture(itexture,SColor(alpha,red,green,blue) );
	itexture->regenerateMipMapLevels();

	return true;
	}


//////////////////////////////////////////////////
MDLLFUN bool CGLTextureColorKeyPixel(int textureid,int pixelx,int pixely)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	MCGLTextureInfo *texture=GlobalTextureList.Find(textureid);
	if(texture==NULL)
		{
		MStdPrintf("**Unable to find texture %d\n",textureid);
		return false;
		}

	GlobalRefVideoDriver->makeColorKeyTexture(texture->GetTexture(),position2d<int>(pixelx,pixely) );
	texture->GetTexture()->regenerateMipMapLevels();

	return true;	
	}

